package week9;

import javax.swing.*;

/**
 * @author The setting sun
 * @version 1.0
 * @title: ThreadDemo
 * @description: TODO
 * @date 2021/11/3 9:33
 */
public class Work2 {
    public static void main(String[] args) {
        new MyFrame().init();
    }
}

class MyFrame {
    JFrame jf;
    JLabel label;

    public MyFrame() {
        jf = new JFrame("多线程可以实现动画效果");
        label = new JLabel("多线程可以实现动画效果");
    }

    public void init() {
        jf.setLayout(null);
        label.setBounds(20, 400, 200, 50);
        jf.add(label);
        jf.setVisible(true);
        jf.setSize(600, 600);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //创建线程 并启动
        class MyThread extends Thread {
            public void run() {
                int x = 10;
                int i = 1;
                boolean rEdge = false;  //是否到底右边界
                while (true) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    label.setBounds(20 + x * i, 400, 200, 50);
                    if (rEdge == false)
                        i++;
                    else
                        i--;
                    if (i > 38)
                        rEdge = true;
                    if (i < 0)
                        rEdge = false;
                }
            }
        }

        new MyThread().start();
    }
}

